# ╭──────────────────────────────────────────────────────────╮
# │                         Options                          │
# ╰──────────────────────────────────────────────────────────╯

# Prefix settings
set -g mode-keys vi                    # 在复制模式中使用vi键位绑定
set -g history-limit 10000             # 设置回滚缓冲区行数为10000行
set -g base-index 1                    # 窗口编号从1开始（而非默认的0）
# set -g pane-base-index 1               # 窗格编号从1开始
set -g allow-rename on                 # 允许TMUX自动重命名窗口
set-option -g prefix C-b               # 设置主前缀键为 Ctrl+b[1,4](@ref)
set-option -g prefix2 `                # 设置次要前缀键
set -g mouse on                        # 开启鼠标支持
set-option -g set-clipboard on         # 设置剪贴板
set -g default-shell "$SHELL"          # 设置默认 shell
set -g default-terminal "${TERM}"      # 设置默认终端类型[2](@ref)
set -sg terminal-overrides ",*:RGB"    # 终端覆盖设置，支持 RGB 颜色
set -g renumber-windows on             # 关闭Session后自动重新编号
set -g allow-passthrough on            # 允许直通
set -ga update-environment "TERM TERM_PROGRAM COLORTERM" # 更新环境变量
set -g focus-events on                 # 启用焦点事件检测[1](@ref)
set -sg escape-time 200                # 设置前缀键的响应时间为 200 毫秒[1](@ref)
set -g repeat-time 1000                # 设置重复时间为 1000 毫秒（或 600）
set -g history-limit 100000            # 设置历史记录限制为 100000 行
set -g focus-events off
set -g set-titles on                   # 允许设置终端标题[1](@ref)
set -g focus-events on



# ╭──────────────────────────────────────────────────────────╮
# │                         Keybindings                      │
# ╰──────────────────────────────────────────────────────────╯


unbind r
bind r source-file $HOME/.config/tmux/tmux.conf \; display-message "Config reloaded..."
bind C-b send-prefix
bind ` send-prefix -2

# Vim bindings

bind h previous-window
bind l next-window
bind : command-prompt
bind x kill-pane
bind t set status

# Split windows
bind b split-window -v -c '#{pane_current_path}'
bind v split-window -h -c '#{pane_current_path}'

bind c new-window -c "#{pane_current_path}"
bind -n M-n new-window
bind N new-session


bind-key -n C-h select-pane -L
bind-key -n C-j select-pane -D
bind-key -n C-k select-pane -U
bind-key -n C-l select-pane -R

# bind-key C-h select-pane -L
# bind-key C-j select-pane -D
# bind-key C-k select-pane -U
# bind-key C-l select-pane -R


bind-key n command-prompt "new-session -s '%%'"


# Global keybindings
bind -n M-g display-popup -d "#{pane_current_path}" -xC -yC -w95% -h90% -B -E -e "TMUX_POPUP=1" 'lazygit'
bind -n M-y display-popup -d "#{pane_current_path}" -xC -yC -w95% -h90% -B -E -e "TMUX_POPUP=1" 'yazi'
bind -n M-j display-popup -d "#{pane_current_path}" -xC -yC -w95% -h90% -B -E -e "TMUX_POPUP=1" 'lazyjj'
bind -n M-d split-window -c "#{pane_current_path}" 'gh dash'
bind -n M-s display-popup -d "#{pane_current_path}" -xC -yC -w95% -h90% -E

# Copy mode configuration
# bind-key -T copy-mode-vi 'Escape' send -X cancel
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-selection
bind-key -T copy-mode-vi 'V' send -X select-line
bind-key -T copy-mode-vi 'H' send -X start-of-line
bind-key -T copy-mode-vi 'L' send -X end-of-line


# %if "#{==:$SSH_TTY,}"
#   # 本地会话才执行：为嵌套 tmux 提供 prefix 透传
#   bind ` send-prefix -2
#   bind C-b send-prefix
#   bind ` send-prefix
# %endif

# ╭──────────────────────────────────────────────────────────╮
# │                           Pane                           │
# ╰──────────────────────────────────────────────────────────╯
bind i run-shell " \
if [ $(( \$(tmux display -p '8*#{pane_width}-20*#{pane_height}') )) -lt 0 ]; then \
  tmux splitw -v -c '#{pane_current_path}'; \
else \
  tmux splitw -h -c '#{pane_current_path}'; \
fi"


bind -n S-Up    resize-pane -U 5
bind -n S-Down  resize-pane -D 5
bind -n S-Left  resize-pane -L 5
bind -n S-Right resize-pane -R 5






# ╭──────────────────────────────────────────────────────────╮
# │                         Theme                            │
# ╰──────────────────────────────────────────────────────────╯

# Status bar style
FG_COLOR="#c5c9c5"
BG_COLOR="#1D1C19"
BG_LIGHT="#282727"
BG_ACCENT="#8ea4a2"

STATUS_OPEN="#[fg=${BG_COLOR},bg=default]#[fg=${FG_COLOR},bg=${BG_COLOR}]"
STATUS_CLOSE="#[fg=${BG_COLOR},bg=default]"
TIME_FORMAT="%I:%M %p"

# Use current_path.sh to handle path
STATUS_FORMAT="#(echo '#{pane_current_path}' | ~/.config/tmux/current_path.sh)"
set -g status-left ""
set -g status-left-length 200
set -g status-right "${STATUS_OPEN}${STATUS_FORMAT}${STATUS_CLOSE} ${STATUS_OPEN}${TIME_FORMAT}${STATUS_CLOSE}"
set -g status-interval 5
set -g status-bg default
set -g status-style bg=default

WINDOW_FORMAT="#I"
ACTIVE_WINDOW_FORMAT="#{?window_zoomed_flag, ,} #I"
WINDOW_STATUS_OPEN="#[fg=${BG_LIGHT},bg=default]#[fg=${FG_COLOR},bg=${BG_LIGHT}]"
WINDOW_STATUS_CLOSE=" #[fg=${FG_COLOR},bg=${BG_LIGHT}]${WINDOW_FORMAT}#[fg=${BG_LIGHT},bg=default]"
ACTIVE_WINDOW_STATUS_OPEN="#[fg=${BG_LIGHT},bg=default]#[fg=${FG_COLOR},bg=${BG_LIGHT}]"
ACTIVE_WINDOW_STATUS_CLOSE=" #[fg=${FG_COLOR},bg=${BG_ACCENT}]${ACTIVE_WINDOW_FORMAT}#[fg=${BG_ACCENT},bg=default]"

set -g status-position top
set -g window-status-current-format "${ACTIVE_WINDOW_STATUS_OPEN}#W${ACTIVE_WINDOW_STATUS_CLOSE}"
set -g window-status-format "${WINDOW_STATUS_OPEN}#W${WINDOW_STATUS_CLOSE}"
set -g message-command-style bg=default,fg=yellow
set -g message-style bg=default,fg=yellow
set -g mode-style bg=default,fg=yellow
set -g pane-active-border-style "fg=${BG_ACCENT},bg=default"
set -g pane-border-style "fg=${BG_LIGHT},bg=default"
